<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="1100px">
    <ContentWrap>
      <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
        <el-form-item label="类别名称" prop="categoryName">
          <el-input
            v-model="queryParams.categoryName"
            placeholder="请输入类别名称"
            :clearable="true"
            @keyup.enter="getPage"
          />
        </el-form-item>
        <el-form-item label="是否初始收费" prop="isFirst">
          <el-select v-model="queryParams.isFirst">
            <el-option
              v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button @click="handleClickSearch">
            <Icon icon="ep:search" />
            搜索
          </el-button>
          <el-button @click="handleClickReset">
            <Icon icon="ep:refresh" />
            重置
          </el-button>
          <el-button @click="saveValues"> 保存 </el-button>
        </el-form-item>
      </el-form>
      <Table
        :columns="columns"
        :page-param="queryParams"
        :adaptive="true"
        :page-data="queryData"
        save-key="FeeCategorySelect"
        @page-change="getPage"
        @row-dblclick="handleTableDblclick"
      />
    </ContentWrap>
    <ContentWrap class="h-[230px]">
      <Table :columns="columns" :data="selectInfoList" @row-dblclick="handleSelectTableDblclick">
        <template #disable="{ row }">
          {{ row.type == 0 ? '否' : '是' }}
        </template>
      </Table>
    </ContentWrap>
  </Dialog>
</template>
<script setup name="FeeCategorySelect" lang="ts">
import * as FeeCategoryApi from '@/api/fee/feeCategory'
import * as FeeCategoryModel from '@/model/fee/category/FeeCategory'
import { DICT_TYPE, getBoolDictOptions, getDictObj } from '@/utils/dict'
import { getKeyList } from '@pureadmin/utils'
const selectInfoList = ref<FeeCategoryModel.FeeCategory[]>([])
const dialogTitle = ref('')
const dialogVisible = ref(false)
const queryFormRef = ref()
const queryParams = ref({
  categoryName: undefined,
  isBuiltin: false,
  isFirst: undefined
})
const emit = defineEmits(['selectValue'])
const queryData = ref()
const columns = [
  {
    label: '类别名称',
    prop: 'categoryName'
  },
  {
    label: '类型',
    prop: 'type',
    formatter: ({ type }) => getDictObj(DICT_TYPE.FEE_CATEGORY_TYPE, type).label
  },
  {
    label: '是否内置',
    prop: 'isBuiltin',
    formatter: ({ isBuiltin }) => getDictObj(DICT_TYPE.INFRA_BOOLEAN_STRING, isBuiltin).label
  },
  {
    label: '是否初始收费',
    prop: 'isFirst',
    formatter: ({ isFirst }) => getDictObj(DICT_TYPE.INFRA_BOOLEAN_STRING, isFirst).label
  },
  {
    label: '费用',
    prop: 'fee',
    slot: 'fee'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]
/** 打开弹窗 */
const open = () => {
  dialogVisible.value = true
  selectInfoList.value = []
  getPage()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await FeeCategoryApi.query(queryParams.value)
}

/** 双击table列操作  */
const handleTableDblclick = (row) => {
  const existsIds = getKeyList(selectInfoList.value, 'id')
  if (existsIds.indexOf(row.id) < 0) selectInfoList.value.push(row)
}

const handleSelectTableDblclick = (row) => {
  const index = selectInfoList.value.indexOf(row)
  selectInfoList.value.splice(index, 1)
}

const saveValues = () => {
  emit('selectValue', selectInfoList.value)
  close()
}

/** 关闭弹窗 */
const close = async () => {
  dialogVisible.value = false
}
</script>
